<template>
  <div class="admin-sidebar">
    <!-- 系统Logo -->
    <div class="sidebar-logo">
      <span v-show="!isCollapse">博客管理系统</span>
      <span v-show="isCollapse">系统</span>
    </div>

    <!-- 导航菜单 -->
    <el-menu
      :default-active="activeMenu"
      :collapse="isCollapse"
      :collapse-transition="false"
      class="sidebar-menu"
      @select="handleMenuSelect"
    >
      <!-- 文章管理 -->
      <el-menu-item index="/admin/articles">
        <el-icon class="menu-icon"><Menu /></el-icon>
        <template #title>
          <span class="menu-text">文章管理</span>
        </template>
      </el-menu-item>

      <!-- 分类管理 -->
      <el-menu-item index="/admin/categories">
        <el-icon class="menu-icon"><Folder /></el-icon>
        <template #title>
          <span class="menu-text">分类管理</span>
        </template>
      </el-menu-item>

      <!-- 标签管理 -->
      <el-menu-item index="/admin/tags">
        <el-icon class="menu-icon"><Tag /></el-icon>
        <template #title>
          <span class="menu-text">标签管理</span>
        </template>
      </el-menu-item>

      <!-- 评论管理 -->
      <el-menu-item index="/admin/comments">
        <el-icon class="menu-icon"><Message /></el-icon>
        <template #title>
          <span class="menu-text">评论管理</span>
        </template>
      </el-menu-item>

      <!-- 媒体管理 -->
      <el-menu-item index="/admin/media">
        <el-icon class="menu-icon"><Picture /></el-icon>
        <template #title>
          <span class="menu-text">媒体管理</span>
        </template>
      </el-menu-item>

      <!-- 页面管理 -->
      <el-menu-item index="/admin/pages">
        <el-icon class="menu-icon"><Document /></el-icon>
        <template #title>
          <span class="menu-text">页面管理</span>
        </template>
      </el-menu-item>

      <!-- 友情链接 -->
      <el-menu-item index="/admin/links">
        <el-icon class="menu-icon"><Link /></el-icon>
        <template #title>
          <span class="menu-text">友情链接</span>
        </template>
      </el-menu-item>

      <!-- 访问统计 -->
      <el-menu-item index="/admin/statistics">
        <el-icon class="menu-icon"><DataAnalysis /></el-icon>
        <template #title>
          <span class="menu-text">访问统计</span>
        </template>
      </el-menu-item>

      <!-- 订阅管理 -->
      <el-menu-item index="/admin/subscriptions">
        <el-icon class="menu-icon"><Bell /></el-icon>
        <template #title>
          <span class="menu-text">订阅管理</span>
        </template>
      </el-menu-item>

      <!-- 系统设置 -->
      <el-menu-item index="/admin/settings">
        <el-icon class="menu-icon"><Setting /></el-icon>
        <template #title>
          <span class="menu-text">系统设置</span>
        </template>
      </el-menu-item>

      <!-- 同步日志 -->
      <el-menu-item index="/admin/sync-logs">
        <el-icon class="menu-icon"><RefreshRight /></el-icon>
        <template #title>
          <span class="menu-text">同步日志</span>
        </template>
      </el-menu-item>
    </el-menu>

    <!-- 折叠按钮 -->
    <div class="collapse-btn" @click="toggleCollapse">
      <el-icon>
        <component :is="isCollapse ? Expand : Fold" />
      </el-icon>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

// 解构出需要的图标
const {
  Menu,
  Folder,
  Message,
  Picture,
  Document,
  Link,
  DataAnalysis,
  Bell,
  Setting,
  RefreshRight, // 使用 RefreshRight 替代 Sync
  Expand,
  Fold
} = ElementPlusIconsVue

const router = useRouter()
const route = useRoute()
const isCollapse = ref(false)

// 当前激活菜单
const activeMenu = computed(() => route.path)

// 菜单选择处理
const handleMenuSelect = (index: string) => {
  router.push(index)
}

// 切换折叠状态
const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
}
</script>

<style scoped>
.admin-sidebar {
  position: relative;
  height: 100vh;
  background: #304156;
  box-shadow: 2px 0 6px rgba(0, 0, 0, 0.1);
  transition: width 0.3s;
}

/* Logo样式 */
.sidebar-logo {
  height: 60px;
  line-height: 60px;
  text-align: center;
  color: #fff;
  font-size: 18px;
  font-weight: 600;
  background: #2b2f3a;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

/* 菜单容器 */
.sidebar-menu {
  border-right: none;
  height: calc(100vh - 120px);
  overflow-x: hidden;
  overflow-y: auto;
}

/* 菜单项样式 */
:deep(.el-menu-item) {
  height: 48px;
  line-height: 48px;
  margin: 4px 0;
  color: #bcc9d4;
  transition: all 0.2s;
}

/* 图标样式 */
:deep(.el-menu-item) .menu-icon {
  font-size: 18px;
  color: inherit;
  margin-right: 12px;
}

/* 文字样式 */
:deep(.el-menu-item) .menu-text {
  font-size: 14px;
  opacity: 1 !important;
  transition: none;
}

/* 悬停状态 */
:deep(.el-menu-item:hover) {
  background: #263445 !important;
  color: #fff !important;
}

/* 激活状态 */
:deep(.el-menu-item.is-active) {
  background: #1890ff !important;
  color: #fff !important;
}

/* 折叠按钮 */
.collapse-btn {
  position: absolute;
  bottom: 20px;
  left: 0;
  width: 100%;
  height: 40px;
  line-height: 40px;
  text-align: center;
  color: #fff;
  cursor: pointer;
  transition: all 0.3s;
}

.collapse-btn:hover {
  background: #263445;
}

.collapse-btn .el-icon {
  font-size: 18px;
}

/* 响应式处理 */
@media screen and (max-width: 992px) {
  .admin-sidebar {
    width: 64px !important;
  }

  .sidebar-logo span:first-child {
    display: none;
  }

  .sidebar-menu :deep(.el-menu-item) .menu-text {
    display: inline-block !important;
    opacity: 0;
    width: 0;
    transition: opacity 0.3s;
  }

  .sidebar-menu :deep(.el-menu-item:hover) .menu-text {
    opacity: 1;
    width: auto;
    padding-left: 12px;
  }

  .admin-sidebar:hover {
    width: 200px !important;
  }

  .admin-sidebar:hover .sidebar-logo span:first-child {
    display: inline;
  }

  .admin-sidebar:hover .sidebar-menu :deep(.el-menu-item) .menu-text {
    opacity: 1;
    width: auto;
  }
}
</style>